package com.ct.gd.passport.view.passport;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.common.mock.ConnectAsyncTask;
import com.ct.gd.passport.common.mock.ConnectRequest;
import com.ct.gd.passport.common.mock.IConnectable;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.helper.ToastHelper;

public class PassportQueryActivity extends Activity implements OnClickListener {

	EditText mEtApplyId;
	Button mBtnQuery;
	TextView mTvQueryTitle;
	TextView mTvQueryResult;
	
    protected ImageButton		mBtnLeft;      // 头部左边图标按钮
    
    protected ImageButton		mBtnRight;      // 头部右边图标按钮
    
    protected TextView      mHeaderTitleTv;    // 头部标题文字

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.passport_query);
		
    	mBtnLeft = (ImageButton) findViewById(R.id.btn_header_left);
    	mBtnRight = (ImageButton) findViewById(R.id.btn_header_right);
    	mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_center);
    	mHeaderTitleTv.setText(R.string.passport_query);
    	
    	mBtnLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PassportQueryActivity.this.finish();
			}
		});

		mEtApplyId = (EditText) findViewById(R.id.query_applyid);
		mBtnQuery = (Button) findViewById(R.id.query_btn_query);
		mTvQueryTitle = (TextView) findViewById(R.id.query_tv_title);
		mTvQueryResult = (TextView) findViewById(R.id.query_tv_result);

		mBtnQuery.setOnClickListener(this);
		
		if(null != getIntent().getExtras()){
			String number = getIntent().getExtras().getString("number");
			if(StringUtils.isNotBlank(number)){
				mEtApplyId.setText(number);
			}
		}
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.query_btn_query:
			MobclickAgent.onEvent(this, UmengVar.PASSPORT_CODE_QUERY_SEND);
			queryApply(mEtApplyId.getText().toString());
			break;
		}
	}

	public void queryApply(final String id) {
		if(id == null || id.length()<4){
			mTvQueryResult.setText(R.string.INPUT_CONTENT_ERROR);
			return;
		}
		mTvQueryTitle.setVisibility(View.VISIBLE);
		mTvQueryResult.setVisibility(View.VISIBLE);
		try {
			mTvQueryResult.setText(R.string.QUERYING);
			ConnectRequest request = new ConnectRequest();
			request.url = "http://crj.gdga.gov.cn/wsyw/tcustomer/tcustomer.do";
			request.addParams("method", "find");
			request.addParams("applyid", id);
			ConnectAsyncTask cat = new ConnectAsyncTask(new IConnectable() {
				public void onConnectComplete(String response) {
					final String TXT = "受理编号为";
					String regEx = "<([^>]*)>([^>]*)"+TXT+"([^<]*)</([^>]*)>";
					Pattern p = Pattern.compile(regEx);
					Matcher m = p.matcher(response);
					
					StringBuilder sb = new StringBuilder();
					int idx = 1;
					while(m.find()){
						if(sb.length()>0){
							sb.append("\n\n");
						}
						sb.append("第"+idx+"条记录：");
						String resp = (m.group(2)+TXT+m.group(3)).trim();
						sb.append(resp);
						
						idx++;
					}
					if(sb.length()>0){
						mTvQueryResult.setText(sb.toString());
					}else{
						mTvQueryResult.setText(R.string.QUERY_FAIL);
					}
				}
			});
			cat.execute(request);
		}catch (Exception e) {
			ToastHelper.toast(getApplicationContext(), e.getMessage());
		}

	}

}
